﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Newtonsoft.Json;
using Base.Business;
using Base.Common;

namespace BaseApplication.Controllers
{
    public class MenuController : BaseController
    {
        //
        // GET: /Menu/
        private MenuBLL menuBll = null;

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetMenu()
        {
            string pageSize = Request["PageSize"];
            string pageIndex = Request["PageIndex"];
            string sort = Request["Sort"];
            Guid ParentId = ConvertHelper.GetGuid(Request["ParentId"]);
            string sortfield = string.Empty;
            string sortorder = string.Empty;
            menuBll = new MenuBLL();
            if (sort != null)
            {
                sortfield = sort.Split('$')[0];
                sortorder = sort.Split('$')[1];
            }
            DataSet ds = menuBll.GetMenu(CurrentLanguage, int.Parse(pageSize), int.Parse(pageIndex) + 1, sortfield, sortorder == "1" ? "desc" : "asc", ParentId);
            string json = ConvertHelper.GetJSON(ds);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMenuTree()
        {
            menuBll = new MenuBLL();
            DataTable dt = menuBll.GetAllMenu(Constant.Language.zh_CN);
            string json = JsonConvert.SerializeObject(dt);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {
            string Id = Request.QueryString["ParentId"];
            ViewData["ParentId"] = Id;
            ViewData["ParentName"] = Request["ParentName"];
            return View();
        }

        [HttpPost]
        public ContentResult AddMenu()
        {
            //读值
            string Name = Request["Name"];
            string LinkURL = Request["LinkURL"];
            Guid ParentId = new Guid(Request["ParentId"]);
            string Description = Request["Description"];
            int IsDisplay = Convert.ToInt32(Request["IsDisplay"]);
            int IsMenu = Convert.ToInt32(Request["IsMenu"]);
            //int Sort = 1;
            //if (lMenu.Count > 0)
            //{
            //    Sort += lMenu.Last().Sort;
            //}
            //执行方法
            menuBll = new MenuBLL();
            bool isSucc = menuBll.AddMenu(this.CurrentLanguage, Name, LinkURL, ParentId, Description, IsMenu, IsDisplay);
            return Content(isSucc.ToString());
        }

        [HttpPost]
        public void MoveUpMenu()
        {
            Guid Id = ConvertHelper.GetGuid(Request["Id"]);
            menuBll = new MenuBLL();
            menuBll.MoveMenu(Id, 1);
            //return Content("True");
        }

        [HttpPost]
        public void MoveDownMenu()
        {
            Guid Id = ConvertHelper.GetGuid(Request["Id"]);
            menuBll = new MenuBLL();
            menuBll.MoveMenu(Id, 0);
            //return Content("True");
        }

        [HttpPost]
        public ContentResult DeleteMenu()
        {
            Guid Id = ConvertHelper.GetGuid(Request["Id"]);
            menuBll = new MenuBLL();

            return Content(menuBll.DeleteMenu(Id).ToString());
        }

        public ActionResult Edit()
        {
            Guid Id = ConvertHelper.GetGuid(Request.QueryString["Id"]);
            menuBll = new MenuBLL();
            DataTable dt = menuBll.GetMenuById(Id,CurrentLanguage);
            if (dt.Rows.Count > 0)
            {
                ViewData["Id"] = Id;
                ViewData["ParentName"] = dt.Rows[0]["ParentName"];
                ViewData["MenuName"] = dt.Rows[0]["MenuName"];
                ViewData["IsMenu"] = dt.Rows[0]["IsMenu"];
                ViewData["IsDisplay"] = dt.Rows[0]["IsDisplay"];
                ViewData["Description"] = dt.Rows[0]["Description"];
                ViewData["LinkURL"] = dt.Rows[0]["LinkURL"];
                
            }
            return View();
        }

        [HttpPost]
        public ContentResult UpdateMenu()
        {
            Guid Id =ConvertHelper.GetGuid( Request["Id"]);
            string Name = Request["Name"];
            string LinkURL = Request["LinkURL"];
            string Description = Request["Description"];
            int IsDisplay = Convert.ToInt32(Request["IsDisplay"]);
            int IsMenu = Convert.ToInt32(Request["IsMenu"]);
            menuBll = new MenuBLL();
            bool isSucc = menuBll.UpdateMenu(this.CurrentLanguage, Id,Name, LinkURL, Description, IsMenu, IsDisplay);
            return Content(isSucc.ToString());
        }

    }
}
